package cn.jantd.modules.demo.file.controller;


import cn.jantd.core.annotation.AutoLog;
import cn.jantd.core.api.vo.Result;
import cn.jantd.core.util.RedisUtil;
import cn.jantd.modules.demo.file.entity.SysFile;
import cn.jantd.modules.demo.file.service.ISysFileService;
import cn.jantd.modules.demo.sso.entity.SysSso;
import cn.jantd.modules.demo.sso.service.ISysSsoService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @Author xiagf
 * @date 2019-07-04
 */
@Slf4j
@RestController
@RequestMapping("/test/file")
public class SysFileController {

    @Autowired
    private ISysFileService sysUserService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 添加
     *
     * @param jsonObject
     * @return
     */
    @AutoLog(value = "用户管理-添加")
    @ApiOperation(value = "用户管理-添加")
    @PostMapping(value = "/add")
    public Result<SysFile> add(@RequestBody JSONObject jsonObject) {
        Result<SysFile> result = new Result<>();
        try {
            SysFile user = JSON.parseObject(jsonObject.toJSONString(), SysFile.class);
            // 设置创建时间
            user.setCreateTime(new Date());
            sysUserService.addUser(user);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 编辑
     *
     * @param jsonObject
     * @return
     */

    @AutoLog(value = "用户管理-编辑")
    @ApiOperation(value = "用户管理-编辑")
    @PutMapping(value = "/edit")
    public Result<SysFile> edit(@RequestBody JSONObject jsonObject) {
        Result<SysFile> result = new Result<>();
        try {
            SysFile sysUser = sysUserService.getById(jsonObject.getString("id"));
            if (sysUser == null) {
                result.error500("未找到对应实体");
            } else {
                SysFile user = JSON.parseObject(jsonObject.toJSONString(), SysFile.class);
                user.setUpdateTime(new Date());
                sysUserService.editUser(user);
                result.success("修改成功!");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户管理-通过id查询")
    @ApiOperation(value = "用户管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SysFile> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<SysFile> result = new Result<>();
        SysFile sysUser = sysUserService.getById(id);
        if (sysUser == null) {
            result.setSuccess(false);
        } else {
            result.setResult(sysUser);
            result.setSuccess(true);
        }
        return result;
    }
}
